package com.ruoyi.basicData.ruleSetting.service.impl;

import java.util.List;

import com.ruoyi.basicData.ruleSetting.domain.DocumentRules;
import com.ruoyi.basicData.ruleSetting.mapper.DocumentRulesMapper;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basicData.ruleSetting.mapper.DocumentRulesMapper;
import com.ruoyi.basicData.ruleSetting.domain.DocumentRules;
import com.ruoyi.basicData.ruleSetting.service.IDocumentRulesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 单据规则Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-23
 */
@Service
@RequiredArgsConstructor
public class DocumentRulesServiceImpl implements IDocumentRulesService
{
    private final DocumentRulesMapper documentRulesMapper;
    private final RedisCache redisCache;

    /**
     * 查询单据规则
     * 
     * @param id 单据规则主键
     * @return 单据规则
     */
    @Override
    public DocumentRules selectDocumentRulesById(Long id)
    {
        return documentRulesMapper.selectDocumentRulesById(id);
    }

    /**
     * 查询单据规则列表
     * 
     * @param documentRules 单据规则
     * @return 单据规则
     */
    @Override
    public List<DocumentRules> selectDocumentRulesList(DocumentRules documentRules)
    {
        return documentRulesMapper.selectDocumentRulesList(documentRules);
    }

    /**
     * 新增单据规则
     * 
     * @param documentRules 单据规则
     * @return 结果
     */
    @Override
    public int insertDocumentRules(DocumentRules documentRules)
    {
        return documentRulesMapper.insertDocumentRules(documentRules);
    }

    /**
     * 修改单据规则
     * 
     * @param documentRules 单据规则
     * @return 结果
     */
    @Override
    public int updateDocumentRules(DocumentRules documentRules)
    {
        //删除缓存
        redisCache.deleteObject(CacheConstants.RULE_DOC_ID_KEY +documentRules.getDocumentType());
        redisCache.deleteObject(documentRules.getDocumentType()+"_id");
        return documentRulesMapper.updateDocumentRules(documentRules);
    }

    /**
     * 批量删除单据规则
     * 
     * @param ids 需要删除的单据规则主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDocumentRulesByIds(Long[] ids)
    {
        //根据ID查询documentType
        List<String> docTypes = documentRulesMapper.selectDocTypeByIds(ids);
        redisCache.deleteObject(docTypes);
        return documentRulesMapper.deleteDocumentRulesByIds(ids);
    }

    /**
     * 删除单据规则信息
     * 
     * @param id 单据规则主键
     * @return 结果
     */
    @Override
    public int deleteDocumentRulesById(Long id)
    {
        DocumentRules documentRules = documentRulesMapper.selectDocumentRulesById(id);
        if(documentRules != null) {
            redisCache.deleteObject(CacheConstants.RULE_DOC_ID_KEY + documentRules.getDocumentType());
        }
        return documentRulesMapper.deleteDocumentRulesById(id);
    }

    @Override
    public DocumentRules selectDocumentRulesByDocType(String docType) {
        //根据单据类型找到规则信息
        return documentRulesMapper.selectDocumentRulesByDocType(docType);
    }
}
